package org.example;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    // 歌曲所在的文件夹地址
    public static String musicDir = "D:\\0-111";
    // 请把歌曲的名字设置为歌手-歌名，否则程序会自动跳过，不转换
    // 歌手-歌名
    // 例如: 周杰伦-青花瓷
    // 例如: 周杰伦 - 青花瓷

    // 创建一个单线程化的线程池，它只会用唯一的工作线程来执行任务
    public static ExecutorService singleSearchLocalThreadExecutor = null;
    static {
        singleSearchLocalThreadExecutor = Executors.newSingleThreadExecutor();
    }
    public static void main(String[] args) {
        singleSearchLocalThreadExecutor.execute(new Runnable() {
            @Override
            public void run() {
                getAllFileByDir(musicDir);
            }
        });
    }


    private static void getAllFileByDir(String fileDir) {
        File file = new File(fileDir);
        // 获取目录下的所有文件或文件夹
        File[] files = file.listFiles();
        if (files == null) {
            // 如果目录为空，直接退出
            log("路径为空");
            return;
        }
        if(files.length < 1){
            log("文件夹里面是空的哦");
            return;
        }
        // 遍历，目录下的所有文件
        for (File f : files) {
            if (f.isFile()) {
                if (f.getAbsolutePath().endsWith(".mp3")) {
                    String name = f.getName();
                    name = name.substring(0, name.lastIndexOf("."));
                    String path = f.getAbsolutePath();
                    path = path.replace("\\", "/");
                    changeLocalSourceToMp3(name,path);
                }
            } else if (f.isDirectory()) {
                getAllFileByDir(f.getAbsolutePath());
            }
        }
        log("");
        log("");
        log("转换完成，系统正常退出");
        System.exit(1);
    }

    public static void changeLocalSourceToMp3(String name,String localFilePath) {
        File file = new File(localFilePath);
        try {
            int begin = name.indexOf("-");
            if(begin == -1){
                log(name+" 歌曲名称规则错误，正确的格式为: 歌手-歌名");
                return;
            }
            String author = name.substring(0,begin).replace(" ","");
            String musicName = name.substring(begin,name.length());
            musicName = musicName.replace(" ","");
            if(musicName.startsWith("-")){
                musicName = musicName.substring(1,musicName.length());
            }else if(musicName.startsWith("- ")){
                musicName = musicName.substring(2,musicName.length());
            }else if(musicName.startsWith("-  ")){
                musicName = musicName.substring(3,musicName.length());
            }
            if(musicName.endsWith("-")){
                musicName = musicName.substring(0,musicName.length()-1);
            }else if(musicName.endsWith("- ")){
                musicName = musicName.substring(0,musicName.length()-2);
            }else if(musicName.endsWith("-  ")){
                musicName = musicName.substring(0,musicName.length()-3);
            }
            AudioFile audioFile = AudioFileIO.read(file);
            // 读取音频文件
            Tag tag = audioFile.getTag();
            // 设置标题
            tag.setField(FieldKey.TITLE, musicName);
            // 设置作者
            tag.setField(FieldKey.ARTIST, author);
            // 专辑名，目前用歌曲名代替
            tag.setField(FieldKey.ALBUM, musicName);
            AudioFileIO.write(audioFile);
            log(name+" 修改标签完成");
        } catch (Exception e) {
            e.printStackTrace();
            log(name+" 修改标签失败！！！");
        }

    }

    public static void log(String text){
        System.out.println(text);
    }


}
